package com.longxi.tianjintianbao.ui.presenter.contact;

import android.content.Intent;
import android.net.Uri;

import com.longxi.tianjintianbao.listener.SearchListener;
import com.longxi.tianjintianbao.listener.SelectContactDetailListener;
import com.longxi.tianjintianbao.model.Search;
import com.longxi.tianjintianbao.model.complaint.Complaint;
import com.longxi.tianjintianbao.model.contact.ContactDetail;
import com.longxi.tianjintianbao.ui.adapter.multitype.contact.ContactListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.contact.IContactSearchAtView;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by 70339 on 2018/5/3.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class ContactSearchAtPresenter extends BasePresenter<IContactSearchAtView> {

    private MultiTypeAdapter adapter;
    private Items items;
    public static String CONTACTDETAILLIST = "CONTACTDETAILLIST";//单据Activity之间传值key
    List<ContactDetail> contactDetailList = new ArrayList<>();
    private Complaint complaint;

    public ContactSearchAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            SelectContactDetailListener selectContactDetailListener = new SelectContactDetailListener() {
                @Override
                public void onSuccess(ContactDetail contactDetail) {
                    diallPhone(contactDetail.getPersonnelphone());
                }
            };
            adapter.register(ContactDetail.class, new ContactListViewBinder(selectContactDetailListener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        contactDetailList = (List<ContactDetail>) intent.getSerializableExtra(CONTACTDETAILLIST);
        SearchListener searchListener = new SearchListener() {
            @Override
            public void onSearch(String keyword) {
                if("".equals(keyword)){
                    mContext.finish();
                }else{
                    search(keyword);
                }
            }
        };
        getView().getSearchView().setDate(new Search("请输入联系人姓名"), searchListener, false);
    }

    private void search(String keyword) {
        items = new Items();
        for (ContactDetail contactDetail : contactDetailList) {
            if(contactDetail.getPersonnelname().contains(keyword)){
                items.add(contactDetail);
            }
        }
        if(items.size() == 0){
            mContext.showNoData();
        }else{
            mContext.hideLoading();
            adapter.setItems(items);
            adapter.notifyDataSetChanged();
        }
    }

    public void noSearch() {
        mContext.hideLoading();
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        mContext.startActivity(intent);
    }
}
